﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Drawing;

// 用session一定要实现IRequiresSessionState接口，否则会报错。
public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/JPEG";

        using (Bitmap bitmap = new System.Drawing.Bitmap(100, 30))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                Random random = new Random();
                string code = random.Next(10000, 99999).ToString();
                g.DrawString(code, new Font("ＭＳ Ｐゴシック", 20, FontStyle.Bold), Brushes.Red, new PointF(0, 0));
                //g.DrawEllipse(new Pen(new SolidBrush(Color.Blue), 3), 30, 10, 10, 10);

                HttpContext.Current.Session["code"] = code;
                
                bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}